package sdp.group14.appdata;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * this class is to create the database which will be used 
 * to store the trip's information
 * @author ste706
 *
 */
public class HousingSQLiteOpenHelper extends SQLiteOpenHelper {
	
	
	//private String repDate, repStatus, repCategory, repDescription, repCause, repComment;
	//private boolean repAllowEnter;
	//
	public static final int VERSION = 1;
	public static final String DB_NAME  = "housing_db";
	public static final String REQUESTS_TABLE  = "requests";
	public static final String REQUEST_ID = "request_id";
	public static final String REQUEST_ALLOW_ENTER = "request_allow_enter";
	public static final String REQUEST_STATUS = "request_status";
	public static final String REQUEST_CATEGORY = "request_category";
	public static final String REQUEST_DESCRIPTION = "request_description";
	public static final String REQUEST_CAUSE = "request_cause";
	public static final String REQUEST_COMMENT = "request_comment";
	public static final String REQUEST_DATE = "request_date";
	
	public HousingSQLiteOpenHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
	}

	/**
	 * called when the class is created
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		dropAndCreate(db);
	}

	/**
	 * drop and create the table on updata
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("drop table if exists " + REQUESTS_TABLE + ";");
		createTables(db);
	}

	/**
	 * drop and create the table
	 * @param db
	 */
	protected void dropAndCreate(SQLiteDatabase db) {
		db.execSQL("drop table if exists " + REQUESTS_TABLE + ";");
		createTables(db);
	}
	
	/**
	 * create the table in the database
	 * @param db
	 */
	protected void createTables(SQLiteDatabase db) {
		try
		{			
			db.execSQL(
					"create table " + REQUESTS_TABLE +" (" +
					REQUEST_ID + " integer primary key autoincrement not null," +
					REQUEST_DATE + " text," +
					REQUEST_STATUS + " text," +
					REQUEST_CATEGORY + " text," +
					REQUEST_DESCRIPTION + " text," +
					REQUEST_CAUSE + " text," +
					REQUEST_COMMENT + " text," +
					REQUEST_ALLOW_ENTER + " int" +
					");"
				);
			Log.e("database","database created successfully");
		}
		catch(Exception e)
		{
			Log.e("database","Error occured in create table");
		}
		
	}
}
